package org.ftc.dao; 

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.ftc.domain.Motion;
import org.ftc.form.MotionForm;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

@Repository
public class MotionDao extends CurrentDao <Motion, String> {

	@Override
	public Motion load(String id) {
		return super.load(id);
	}

	@Override
	public Motion get(String id) {
		return super.get(id);
	}
	
	@SuppressWarnings("unchecked")
	public List<Motion> getBySessionNo(String sessionNo){
		try{	
			Criteria criteria = getSession().createCriteria(Motion.class);
			criteria.createAlias("session", "session");
			criteria.add(Restrictions.eq("session.sessionNo", sessionNo));
			criteria.addOrder(Order.asc("typeNo"));
			criteria.addOrder(Order.asc("motionNo"));
			List<Motion> list = (List<Motion>) criteria.list();
			return list; 
		}catch( Exception e ){
			e.printStackTrace();
			getLog().error(e.toString());
			return null;
		}
	}
	
	public Motion getByMotionNo(String motionNo){
		try{	
			Criteria criteria = getSession().createCriteria(Motion.class);
			criteria.createAlias("session", "session");
			criteria.add(Restrictions.eq("motionNo", motionNo));
			return (Motion) criteria.uniqueResult();
		}catch( Exception e ){
			e.printStackTrace();
			getLog().error(e.toString());
			return null;
		}
	}
	
	@Override
	public synchronized boolean save(Motion entity) {
		return super.save(entity);
	}

	@Override
	public synchronized boolean saveOrUpdate(Motion entity) {
		return super.saveOrUpdate(entity);
	}

	@Override
	public synchronized boolean update(Motion entity) {
		return super.update(entity);
	}

	@Override
	public synchronized boolean delete(Motion entity) {
		return super.delete(entity);
	}

	@Override
	public synchronized boolean delete(String id) {
		return super.delete(id);
	}

	@Override
	public List<Motion> findPage(int offset, int pageSize) {
		return super.findPage(offset, pageSize);
	}

	@Override
	public List<Motion> findAll() {
		return super.findAll();
	}

	@Override
	public long countAll() {
		return super.countAll();
	}
	
	@SuppressWarnings("unchecked")
	public List<Motion> findPage(MotionForm form, int offset, int pageSize) {
		try{	
			Criteria criteria = getSession().createCriteria(Motion.class);
			criteria.createAlias("session", "session");
			criteriaWsql(form, criteria);
			criteria.addOrder(Order.desc("session.sessionNo"));
			criteria.addOrder(Order.asc("typeNo"));
//			criteria.addOrder(Order.asc("motionOrder"));
//			criteriaSort(form, criteria);
			criteria.setFirstResult(offset);
			criteria.setMaxResults(pageSize);
			List<Motion> list = (List<Motion>) criteria.list();
			return list; 
		}catch( Exception e ){
			e.printStackTrace();
			getLog().error(e.toString());
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Motion> findPage2(MotionForm form, int offset, int pageSize) {
		try{	
//			Dialect dialect= getDialect();
			Criteria criteria = getSession().createCriteria(Motion.class);
			criteria.createAlias("session", "session");
			criteriaWsql(form, criteria);
			criteria.addOrder(Order.desc("session.sessionNo"));
			criteria.addOrder(Order.asc("motionNo"));
//			criteria.addOrder(new org.hibernate.criterion.Order("motionOrder", true) {
//	            @Override
//	            public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
//	                return dialect instanceof MySQLDialect?"cast(motion_order as DECIMAL(10,2))":"cast(motion_order as int)";
//	            }
//	        });
//			criteriaSort(form, criteria);
			criteria.setFirstResult(offset);
			criteria.setMaxResults(pageSize);
			List<Motion> list = (List<Motion>) criteria.list();
			return list; 
		}catch( Exception e ){
			e.printStackTrace();
			getLog().error(e.toString());
			return null;
		}
	}
	
	public long count(MotionForm form) {
		try{	
			Criteria criteria = getSession().createCriteria(Motion.class); 
		    criteria.setProjection(Projections.rowCount()); 
		    criteriaWsql(form, criteria);
		    return (Long) criteria.uniqueResult();
		}catch( Exception e ){
			getLog().error(e.toString());
			return 0; 
		}
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Motion> findAll(MotionForm form) {
		try{	
			Criteria criteria = getSession().createCriteria(Motion.class);
			criteria.createAlias("session", "session");
			criteriaWsql(form, criteria);
			criteria.addOrder(Order.desc("session.sessionNo"));
//			criteria.addOrder(Order.asc("motionOrder"));
			List<Motion> list = (List<Motion>) criteria.list();
			return list; 
		}catch( Exception e ){
			e.printStackTrace();
			getLog().error(e.toString());
			return null;
		}
	}
	
	private void criteriaWsql(MotionForm form, Criteria criteria){
		if(StringUtils.isNotBlank(form.getSessionNo())&&StringUtils.isNotBlank(form.getSessionNo2())){
			criteria.add(Restrictions.between("session.sessionNo", form.getSessionNo(), form.getSessionNo2() ));
		}else{
			if(StringUtils.isNotBlank(form.getSessionNo())){
				criteria.add(Restrictions.eq("session.sessionNo", form.getSessionNo()));
			}
		}
		if( StringUtils.isNotBlank(form.getUnitCode())){
			criteria.add(Restrictions.eq("unitCode", form.getUnitCode()));
		}
	}
	
}
